﻿
namespace SistemaLogistica.Windows
{
    using System;
    using System.IO;
    using System.Windows.Forms;
    using SistemaLogistica.Business.Entities;
    using SistemaLogistica.Business.Logic;
    using SistemaLogistica.Windows.Helpers;
    using SistemaLogistica.Windows.Panels;
    using WeifenLuo.WinFormsUI.Docking;
    using WindowsControls.VB.Office2007Renderer;

    public partial class MainForm : Form
    {
        //private bool _bSaveLayout = true;
        private DeserializeDockContent deserializeDockContent;
        private FrmExplorer explorador;
        private IRefrescar pantallaActual = null;

        public MainForm()
        {
            InitializeComponent();
            ToolStripManager.Renderer = new Office2007Renderer();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            if (DesignMode == false)
            {
                LoadExplorer();
                InitialLoad();

                FrmDocument.ShowedWindow += new EventHandler<EventArgs>(FrmDocument_ShowedWindow);
            }
        }

        private void FrmDocument_ShowedWindow(object sender, EventArgs e)
        {
            UserControl userControl = (UserControl)sender;
            if (userControl != null)
            {
                pantallaActual = (IRefrescar)userControl;
            }
        }

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing)
            {
                if (MessageBox.Show(this, "¿Esta seguro de Salir del Sistema?", Constantes.TituloMensaje, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
                {
                    //string configFile = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "DockPanel.config");
                    //if (_bSaveLayout)
                    //{
                    //    dockPanel.SaveAsXml(configFile);
                    //}
                    //else if (File.Exists(configFile))
                    //{
                    //    File.Delete(configFile);
                    //}
                    Application.Exit();
                }
                else
                {
                    e.Cancel = true;
                }
            }
        }

        private void tsmExit_Click(object sender, EventArgs e)
        {
            MainForm_FormClosing(this, new FormClosingEventArgs(CloseReason.ApplicationExitCall, true));
        }

        private void tmsPanelExplorador_Click(object sender, EventArgs e)
        {
            try
            {
                this.explorador.Show(this.dockPanel);
            }
            catch
            {
                this.explorador = new FrmExplorer();
                this.explorador.Show(this.dockPanel);
            }
        }

        private void tsmBaraHerramientas_Click(object sender, EventArgs e)
        {
            _mainToolStrip.Visible = this.tsmBaraHerramientas.Checked;
        }

        private void tsmBarraEstado_Click(object sender, EventArgs e)
        {
            _mainStatusStrip.Visible = this.tsmBarraEstado.Checked;
        }

        private void tsmPermisos_Click(object sender, EventArgs e)
        {
            FrmPermisos frm = new FrmPermisos();
            frm.ShowDialog();
        }

        private void tsmOpciones_Click(object sender, EventArgs e)
        {
            FrmOptions frmOptions = new FrmOptions();
            if (frmOptions.ShowDialog() == DialogResult.OK)
            {
                EmpresaManager.Instancia.Actualizar("EmpresaInfo.xml");
                InitialLoad();
            }
        }

        private void tsmAutores_Click(object sender, EventArgs e)
        {
            FrmAboutUs frmAbout = new FrmAboutUs();
            frmAbout.ShowDialog();
        }

        private void btnRefrescar_Click(object sender, EventArgs e)
        {
            if (pantallaActual != null)
            {
                pantallaActual.Refrescar();
            }
        }

        private void LoadExplorer()
        {
            this.explorador = new FrmExplorer();
            this.deserializeDockContent += new DeserializeDockContent(GetDockContent);
            string configFile = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "DockPanel.config");
            if (File.Exists(configFile))
            {
                this.dockPanel.LoadFromXml(configFile, deserializeDockContent);
            }
        }

        private void InitialLoad()
        {
            this.Text = Empresa.RazonSocial + " - SYSTEM";
            this.lblRazonSocial.Text = Empresa.RazonSocial;
            this.lblRUC.Text = Empresa.RUC;
            this.lblRubro.Text = Empresa.Rubro;
            this.lblDescripcion.Text = Empresa.Ubicacion + ", " + Empresa.Direccion + " / " + Empresa.Telefono;
            this.tsmPermisos.Visible = Empresa.Usuario == null ? false : Empresa.Usuario.EsAdministrador;
            this.lblCargo.Text = Empresa.Usuario.EsAdministrador ? "Administrador: " : "Usuario: ";
            this.lblUsuario.Text = Empresa.Usuario.ToString() + "  (" + Empresa.Usuario.Username + ")";
        }

        private IDockContent GetDockContent(string className)
        {
            if (className == typeof(FrmExplorer).ToString())
            {
                return this.explorador;
            }
            else
            {
                return null;
            }
        }
    }
}
